package com.yserp.hg.controller;
import com.jeecg.demo.entity.JeecgDemoEntity;
import com.jeecg.demo.entity.JeecgDemoPage;
import com.yserp.hg.entity.HgOrdrEntity;
import com.yserp.hg.page.HgPage;
import com.yserp.hg.service.HgOrdrServiceI;
import com.yserp.hg.page.HgOrdrPage;

import com.yserp.hg.entity.HgRdr1Entity;
import com.yserp.hg.entity.HgRdr2Entity;
import com.yserp.hg.entity.HgRdr3Entity;

import java.io.Console;
import java.util.ArrayList;
import java.util.List;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.annotations.Case;
import org.apache.log4j.Logger;
import org.jeecgframework.core.util.*;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.jwt.util.GsonUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONArray;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.jwt.util.GsonUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import com.alibaba.fastjson.JSONArray;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Title: Controller
 * @Description: 报关订单
 * @author onlineGenerator
 * @date 2018-04-30 09:03:21
 * @version V1.0
 *
 */
@Api(value="HgOrdr",description="报关订单",tags="hgOrdrController")
@Controller
@RequestMapping("/hgOrdrController")
public class HgOrdrController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(HgOrdrController.class);

	@Autowired
	private HgOrdrServiceI hgOrdrService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	/**
	 * 报关订单列表 页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/yserp/hg/hgOrdrList");
	}
	@RequestMapping(params = "listdz")
	public ModelAndView listdz(HttpServletRequest request) {
		return new ModelAndView("com/yserp/hg/hgOrdrdzList");
	}
	/**
	 * easyui AJAX请求数据
	 *
	 * @param request
	 * @param response
	 * @param dataGrid

	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(HgOrdrEntity hgOrdr,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(HgOrdrEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hgOrdr);
		try{
//			TSUser user =  ResourceUtil.getSessionUser();
//			if(!StringUtil.isEmpty(user.getCurrentDepart().getOrgCode())){
//				cq.like("sysOrgCode",user.getCurrentDepart().getOrgCode()+"%");
//
//			}
			//自定义追加查询条件
			String query_createDate_begin = request.getParameter("createDate_begin1");
			String query_createDate_end = request.getParameter("createDate_end2");
			if(StringUtil.isNotEmpty(query_createDate_begin)){
				cq.ge("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_begin));
			}
			if(StringUtil.isNotEmpty(query_createDate_end)){
				cq.le("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_end));
			}
			String query_updateDate_begin = request.getParameter("updateDate_begin1");
			String query_updateDate_end = request.getParameter("updateDate_end2");
			if(StringUtil.isNotEmpty(query_updateDate_begin)){
				cq.ge("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_begin));
			}
			if(StringUtil.isNotEmpty(query_updateDate_end)){
				cq.le("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_end));
			}
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.hgOrdrService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}


	@RequestMapping(params = "datagriddz")
	public void datagriddz(HgOrdrEntity hgOrdr,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(HgOrdrEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hgOrdr);
		try{
			cq.eq("bpmStatus","1");
//			TSUser user =  ResourceUtil.getSessionUser();
//			if(!StringUtil.isEmpty(user.getCurrentDepart().getOrgCode())){
//				cq.like("sysOrgCode",user.getCurrentDepart().getOrgCode()+"%");
//
//			}
			//自定义追加查询条件
			String query_createDate_begin = request.getParameter("createDate_begin1");
			String query_createDate_end = request.getParameter("createDate_end2");
			if(StringUtil.isNotEmpty(query_createDate_begin)){
				cq.ge("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_begin));
			}
			if(StringUtil.isNotEmpty(query_createDate_end)){
				cq.le("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_end));
			}
			String query_updateDate_begin = request.getParameter("updateDate_begin1");
			String query_updateDate_end = request.getParameter("updateDate_end2");
			if(StringUtil.isNotEmpty(query_updateDate_begin)){
				cq.ge("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_begin));
			}
			if(StringUtil.isNotEmpty(query_updateDate_end)){
				cq.le("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_end));
			}
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.hgOrdrService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	@RequestMapping(params = "listocus")
	public ModelAndView listocus(HttpServletRequest request) {
		return new ModelAndView("com/yserp/hg/hgOrdrocusList");
	}
	@RequestMapping(params = "listcus")
	public ModelAndView listcus(HttpServletRequest request) {
		return new ModelAndView("com/yserp/hg/hgOrdrcusList");
	}
	@RequestMapping(params = "datagridcus")
	public void datagridcus(HgOrdrEntity hgOrdr,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(HgOrdrEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hgOrdr);
		try{
			TSUser user =  ResourceUtil.getSessionUser();
			if(!StringUtil.isEmpty(user.getCurrentDepart().getOrgCode())){
				cq.like("sysOrgCode",user.getCurrentDepart().getOrgCode()+"%");

			}

			//自定义追加查询条件
			String query_createDate_begin = request.getParameter("createDate_begin1");
			String query_createDate_end = request.getParameter("createDate_end2");
			if(StringUtil.isNotEmpty(query_createDate_begin)){
				cq.ge("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_begin));
			}
			if(StringUtil.isNotEmpty(query_createDate_end)){
				cq.le("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_end));
			}
			String query_updateDate_begin = request.getParameter("updateDate_begin1");
			String query_updateDate_end = request.getParameter("updateDate_end2");
			if(StringUtil.isNotEmpty(query_updateDate_begin)){
				cq.ge("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_begin));
			}
			if(StringUtil.isNotEmpty(query_updateDate_end)){
				cq.le("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_end));
			}


			//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}

		cq.add();
		this.hgOrdrService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	@RequestMapping(params = "datagridocus")
	public void datagridocus(HgOrdrEntity hgOrdr,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(HgOrdrEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hgOrdr);
		try{
			TSUser user =  ResourceUtil.getSessionUser();
			if(!StringUtil.isEmpty(user.getCurrentDepart().getOrgCode())){
				cq.like("sysOrgCode",user.getCurrentDepart().getOrgCode()+"%");

			}

			//自定义追加查询条件
			String query_createDate_begin = request.getParameter("createDate_begin1");
			String query_createDate_end = request.getParameter("createDate_end2");
			if(StringUtil.isNotEmpty(query_createDate_begin)){
				cq.ge("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_begin));
			}
			if(StringUtil.isNotEmpty(query_createDate_end)){
				cq.le("createDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_createDate_end));
			}
			String query_updateDate_begin = request.getParameter("updateDate_begin1");
			String query_updateDate_end = request.getParameter("updateDate_end2");
			if(StringUtil.isNotEmpty(query_updateDate_begin)){
				cq.ge("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_begin));
			}
			if(StringUtil.isNotEmpty(query_updateDate_end)){
				cq.le("updateDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(query_updateDate_end));
			}


			//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}

		cq.add();
		this.hgOrdrService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	/**
	 * 保存新增/更新的行数据
	 * @param page
	 * @return
	 */
	@RequestMapping(params = "saveRows")
	@ResponseBody
	public AjaxJson saveRows(HgPage page){
		String message = null;
		List<HgOrdrEntity> demos=page.getDemos();
		AjaxJson j = new AjaxJson();
		if(CollectionUtils.isNotEmpty(demos)){
			for(HgOrdrEntity jeecgDemo:demos){
				if (StringUtil.isNotEmpty(jeecgDemo.getId())) {
					HgOrdrEntity t =hgOrdrService.get(HgOrdrEntity.class, jeecgDemo.getId());
					try {
						message = jeecgDemo.getId()+ "被更新成功";
						MyBeanUtils.copyBeanNotNull2Bean(jeecgDemo, t);
						hgOrdrService.saveOrUpdate(t);
						systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return j;
	}


	/**
	 * 更新报关订单
	 *

	 * @return
	 */
	@RequestMapping(params = "dojieshou")
	@ResponseBody
	public AjaxJson dojieshou(HgOrdrEntity hgOrdr,HgOrdrPage hgOrdrPage, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String message = "接单成功";
		try{
			hgOrdr = hgOrdrService.get(HgOrdrEntity.class,hgOrdr.getId());
			hgOrdr.setDocstatus("已接单");
			hgOrdrService.updateEntitie(hgOrdr);
		}catch(Exception e){
			e.printStackTrace();
			message = "接单失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	@RequestMapping(params = "dochayan")
	@ResponseBody
	public AjaxJson dochayan(HgOrdrEntity hgOrdr,HgOrdrPage hgOrdrPage, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String message = "查验成功";
		try{
			hgOrdr = hgOrdrService.get(HgOrdrEntity.class,hgOrdr.getId());
			hgOrdr.setDocstatus("查验");
			hgOrdrService.updateEntitie(hgOrdr);
		}catch(Exception e){
			e.printStackTrace();
			message = "查验失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	@RequestMapping(params = "dofangxing")
	@ResponseBody
	public AjaxJson dofangxing(HgOrdrEntity hgOrdr,HgOrdrPage hgOrdrPage, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String message = "放行成功";
		try{
			hgOrdr = hgOrdrService.get(HgOrdrEntity.class,hgOrdr.getId());
			hgOrdr.setDocstatus("放行");
			hgOrdrService.updateEntitie(hgOrdr);
		}catch(Exception e){
			e.printStackTrace();
			message = "放行失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}



	/**

	 * 删除报关订单

	 *

	 * @return

	 */

	@RequestMapping(params = "doDel")

	@ResponseBody

	public AjaxJson doDel(HgOrdrEntity hgOrdr, HttpServletRequest request) {

		AjaxJson j = new AjaxJson();

		hgOrdr = systemService.getEntity(HgOrdrEntity.class, hgOrdr.getId());

		String message = "报关订单删除成功";

		try{

			try{

				String  sql = "call p_hgdel('"+ hgOrdr.getId()+"')";    //1 all

				systemService.executeSql(sql);



			} catch (Exception e) {

				e.printStackTrace();

			}

			try{

				if(hgOrdr.getDocstatus().equals("未接单")){

					hgOrdrService.delMain(hgOrdr);

					systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

				}else{

					message = "报关订单已接单，删除失败";

				}

			}catch (Exception e){

				message = "报关订单删除失败";

			}





		}catch(Exception e){

			e.printStackTrace();

			message = "报关订单删除失败";

			throw new BusinessException(e.getMessage());

		}

		j.setMsg(message);

		return j;

	}

	/**

	 * 批量删除报关订单

	 *

	 * @return

	 */

	@RequestMapping(params = "dodz")

	@ResponseBody

	public AjaxJson dodz(String ids,HttpServletRequest request){

		AjaxJson j = new AjaxJson();

		String message = "报关订单生成对账成功";
		try{

				String  sql = "call p_ardz('"+ ids+"')";    //1 all

				systemService.executeSql(sql);

		}catch(Exception e){

			e.printStackTrace();

			message = "报关订单生成对账失败";

			throw new BusinessException(e.getMessage());

		}

		j.setMsg(message);

		return j;

	}

	/**

	 * 批量删除报关订单

	 *

	 * @return

	 */

	@RequestMapping(params = "doBatchDel")

	@ResponseBody

	public AjaxJson doBatchDel(String ids,HttpServletRequest request){

		AjaxJson j = new AjaxJson();

		String message = "报关订单删除成功";

		try{

			for(String id:ids.split(",")){

				HgOrdrEntity hgOrdr = systemService.getEntity(HgOrdrEntity.class,

						id

				);





				try{

					String  sql = "call p_hgdel('"+ hgOrdr.getId()+"')";    //1 all

					systemService.executeSql(sql);



				} catch (Exception e) {

					e.printStackTrace();

				}

				try{

					if(hgOrdr.getDocstatus().equals("未接单")){

						hgOrdrService.delMain(hgOrdr);

						systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

					}else{

						message = "报关订单已接单，删除失败";

					}

				}catch (Exception e){

					message = "报关订单删除失败";

				}





//				hgOrdrService.delMain(hgOrdr);

//				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

			}

		}catch(Exception e){

			e.printStackTrace();

			message = "报关订单删除失败";

			throw new BusinessException(e.getMessage());

		}

		j.setMsg(message);

		return j;

	}




	/**
	 * 删除报关订单
	 *
	 * @return
	 */
	/**
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(HgOrdrEntity hgOrdr, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		hgOrdr = systemService.getEntity(HgOrdrEntity.class, hgOrdr.getId());
		String message = "报关订单删除成功";
		try{
			try{
				String  sql = "call p_hgdel('"+ hgOrdr.getId()+"')";    //1 all
				systemService.executeSql(sql);

			} catch (Exception e) {
				e.printStackTrace();
			}
			hgOrdrService.delMain(hgOrdr);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "报关订单删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
*/
	/**
	 * 批量删除报关订单
	 *
	 * @return
	 */
	/*----cancel
	@RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		String message = "报关订单删除成功";
		try{
			for(String id:ids.split(",")){
				HgOrdrEntity hgOrdr = systemService.getEntity(HgOrdrEntity.class,
						id
				);
				hgOrdrService.delMain(hgOrdr);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "报关订单删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
---*/
	/**
	 * 添加报关订单
	 *

	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(HgOrdrEntity hgOrdr,HgOrdrPage hgOrdrPage, HttpServletRequest request) {
		List<HgRdr1Entity> hgRdr1List =  hgOrdrPage.getHgRdr1List();
		List<HgRdr2Entity> hgRdr2List =  hgOrdrPage.getHgRdr2List();

		List<HgRdr3Entity> hgRdr3List =  hgOrdrPage.getHgRdr3List();

		String ss = request.getParameter("filetype0thelist");
		AjaxJson j = new AjaxJson();
		String message = "添加成功";
		try{
			hgOrdr.setDocstatus("未接单");
			if(!StringUtil.isEmpty(hgOrdr.getFilename0())){
				hgOrdr.setFiletype0(DateUtils.getDataString(DateUtils.datetimeFormat));
			}
			hgOrdrService.addMain(hgOrdr, hgRdr1List,hgRdr2List,hgRdr3List);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "报关订单添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 更新报关订单
	 *

	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(HgOrdrEntity hgOrdr,HgOrdrPage hgOrdrPage, HttpServletRequest request) {
		List<HgRdr1Entity> hgRdr1List =  hgOrdrPage.getHgRdr1List();
		List<HgRdr2Entity> hgRdr2List =  hgOrdrPage.getHgRdr2List();
		List<HgRdr3Entity> hgRdr3List =  hgOrdrPage.getHgRdr3List();

		AjaxJson j = new AjaxJson();
		String message = "更新成功";
		try{
			try{
				String  sql = "call p_hgupd('"+ hgOrdr.getId()+"')";    //1 all
				systemService.executeSql(sql);

			} catch (Exception e) {
				e.printStackTrace();
			}
			hgOrdrService.updateMain(hgOrdr, hgRdr1List,hgRdr2List,hgRdr3List);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "更新报关订单失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 报关订单新增页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(HgOrdrEntity hgOrdr, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(hgOrdr.getId())) {
			hgOrdr = hgOrdrService.getEntity(HgOrdrEntity.class, hgOrdr.getId());

		}
//		TSUser user = ResourceUtil.getSessionUser().getCurrentDepart().getSysOrgCode()
		hgOrdr.setSysOrgCode(ResourceUtil.getSessionUser().getCurrentDepart().getOrgCode());
		req.setAttribute("hgOrdrPage", hgOrdr);
		return new ModelAndView("com/yserp/hg/hgOrdr-add");
	}
	/**
	 * 报关订单新增页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "goAddin")
	public ModelAndView goAddin(HgOrdrEntity hgOrdr, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(hgOrdr.getId())) {
			hgOrdr = hgOrdrService.getEntity(HgOrdrEntity.class, hgOrdr.getId());

		}
//		TSUser user = ResourceUtil.getSessionUser().getCurrentDepart().getSysOrgCode()
		hgOrdr.setSysOrgCode(ResourceUtil.getSessionUser().getCurrentDepart().getOrgCode());
		req.setAttribute("hgOrdrPage", hgOrdr);
		return new ModelAndView("com/yserp/hg/hgOrdr-addin");
	}
	/**
	 * 报关订单编辑页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(HgOrdrEntity hgOrdr, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(hgOrdr.getId())) {
			hgOrdr = hgOrdrService.getEntity(HgOrdrEntity.class, hgOrdr.getId());
			req.setAttribute("hgOrdrPage", hgOrdr);

		}
		return new ModelAndView("com/yserp/hg/hgOrdr-update");
	}
	@RequestMapping(params = "goUpdatein")
	public ModelAndView goUpdatein(HgOrdrEntity hgOrdr, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(hgOrdr.getId())) {
			hgOrdr = hgOrdrService.getEntity(HgOrdrEntity.class, hgOrdr.getId());
			req.setAttribute("hgOrdrPage", hgOrdr);

		}
		return new ModelAndView("com/yserp/hg/hgOrdr-updatein");
	}

	/**
	 * 加载明细列表[报关明细]
	 *
	 * @return
	 */
	@RequestMapping(params = "hgRdr1List")
	public ModelAndView hgRdr1List(HgOrdrEntity hgOrdr, HttpServletRequest req) {

		//===================================================================================
		//获取参数
		Object dOCENTRY0 = hgOrdr.getId();
		//===================================================================================
		//查询-报关明细
		String hql0 = "from HgRdr1Entity where 1 = 1 AND dOCENTRY = ? ";
		try{
			List<HgRdr1Entity> hgRdr1EntityList = systemService.findHql(hql0,dOCENTRY0);
			req.setAttribute("hgRdr1List", hgRdr1EntityList);
		}catch(Exception e){
			logger.info(e.getMessage());
		}
		return new ModelAndView("com/yserp/hg/hgRdr1List");
	}
	/**
	 * 加载明细列表[报关附件]
	 *
	 * @return
	 */
	@RequestMapping(params = "hgRdr2List")
	public ModelAndView hgRdr2List(HgOrdrEntity hgOrdr, HttpServletRequest req) {

		//===================================================================================
		//获取参数
		Object dOCENTRY1 = hgOrdr.getId();
		//===================================================================================
		//查询-报关附件
		String hql1 = "from HgRdr2Entity where 1 = 1 AND dOCENTRY = ? ";
		try{
			List<HgRdr2Entity> hgRdr2EntityList = systemService.findHql(hql1,dOCENTRY1);
			req.setAttribute("hgRdr2List", hgRdr2EntityList);


		}catch(Exception e){
			logger.info(e.getMessage());
		}
		return new ModelAndView("com/yserp/hg/hgRdr2List");
	}
	/**
	 * 加载明细列表[报关文件]
	 *
	 * @return
	 */
	@RequestMapping(params = "hgRdr3List")
	public ModelAndView hgRdr3List(HgOrdrEntity hgOrdr, HttpServletRequest req) {

		//===================================================================================
		//获取参数
		Object dOCENTRY2 = hgOrdr.getId();
		//===================================================================================
		//查询-报关文件
		String hql2 = "from HgRdr3Entity where 1 = 1 AND dOCENTRY = ? ";
		try{
			List<HgRdr3Entity> hgRdr3EntityList = systemService.findHql(hql2,dOCENTRY2);
			req.setAttribute("hgRdr3List", hgRdr3EntityList);

		}catch(Exception e){
			logger.info(e.getMessage());
		}
		return new ModelAndView("com/yserp/hg/hgRdr3List");
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXls")
	public String exportXls(HgOrdrEntity hgOrdr,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid,ModelMap map) {
		CriteriaQuery cq = new CriteriaQuery(HgOrdrEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, hgOrdr);
		try{
			//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		List<HgOrdrEntity> list=this.hgOrdrService.getListByCriteriaQuery(cq, false);
		List<HgOrdrPage> pageList=new ArrayList<HgOrdrPage>();
		if(list!=null&&list.size()>0){
			for(HgOrdrEntity entity:list){
				try{
					HgOrdrPage page=new HgOrdrPage();
					MyBeanUtils.copyBeanNotNull2Bean(entity,page);
					Object dOCENTRY0 = entity.getId();
					String hql0 = "from HgRdr1Entity where 1 = 1 AND dOCENTRY = ? ";
					List<HgRdr1Entity> hgRdr1EntityList = systemService.findHql(hql0,dOCENTRY0);
					page.setHgRdr1List(hgRdr1EntityList);
					Object dOCENTRY1 = entity.getId();
					String hql1 = "from HgRdr2Entity where 1 = 1 AND dOCENTRY = ? ";
					List<HgRdr2Entity> hgRdr2EntityList = systemService.findHql(hql1,dOCENTRY1);
					page.setHgRdr2List(hgRdr2EntityList);
					Object dOCENTRY2 = entity.getId();
					String hql2 = "from HgRdr3Entity where 1 = 1 AND dOCENTRY = ? ";
					List<HgRdr3Entity> hgRdr3EntityList = systemService.findHql(hql2,dOCENTRY2);
					page.setHgRdr3List(hgRdr3EntityList);
					pageList.add(page);
				}catch(Exception e){
					logger.info(e.getMessage());
				}
			}
		}
		map.put(NormalExcelConstants.FILE_NAME,"报关订单");
		map.put(NormalExcelConstants.CLASS,HgOrdrPage.class);
		map.put(NormalExcelConstants.PARAMS,new ExportParams("报关订单列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
				"导出信息"));
		map.put(NormalExcelConstants.DATA_LIST,pageList);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}

	/**
	 * 通过excel导入数据
	 * @param request
	 * @param
	 * @return
	 */
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(2);
			params.setNeedSave(true);
			try {
				List<HgOrdrPage> list =  ExcelImportUtil.importExcel(file.getInputStream(), HgOrdrPage.class, params);
				HgOrdrEntity entity1=null;
				for (HgOrdrPage page : list) {
					entity1=new HgOrdrEntity();
					MyBeanUtils.copyBeanNotNull2Bean(page,entity1);
					hgOrdrService.addMain(entity1, page.getHgRdr1List(),page.getHgRdr2List(),page.getHgRdr3List());
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			}finally{
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}
	/**
	 * 导出excel 使模板
	 */
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(ModelMap map) {
		map.put(NormalExcelConstants.FILE_NAME,"报关订单");
		map.put(NormalExcelConstants.CLASS,HgOrdrPage.class);
		map.put(NormalExcelConstants.PARAMS,new ExportParams("报关订单列表", "导出人:"+ ResourceUtil.getSessionUser().getRealName(),
				"导出信息"));
		map.put(NormalExcelConstants.DATA_LIST,new ArrayList());
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	/**
	 * 导入功能跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name", "hgOrdrController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}


	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="报关订单列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<HgOrdrPage>> list() {
		List<HgOrdrEntity> list= hgOrdrService.getList(HgOrdrEntity.class);
		List<HgOrdrPage> pageList=new ArrayList<HgOrdrPage>();
		if(list!=null&&list.size()>0){
			for(HgOrdrEntity entity:list){
				try{
					HgOrdrPage page=new HgOrdrPage();
					MyBeanUtils.copyBeanNotNull2Bean(entity,page);
					Object dOCENTRY0 = entity.getId();
					Object dOCENTRY1 = entity.getId();
					Object dOCENTRY2 = entity.getId();
					String hql0 = "from HgRdr1Entity where 1 = 1 AND dOCENTRY = ? ";
					List<HgRdr1Entity> hgRdr1OldList = this.hgOrdrService.findHql(hql0,dOCENTRY0);
					page.setHgRdr1List(hgRdr1OldList);
					String hql1 = "from HgRdr2Entity where 1 = 1 AND dOCENTRY = ? ";
					List<HgRdr2Entity> hgRdr2OldList = this.hgOrdrService.findHql(hql1,dOCENTRY1);
					page.setHgRdr2List(hgRdr2OldList);
					String hql2 = "from HgRdr3Entity where 1 = 1 AND dOCENTRY = ? ";
					List<HgRdr3Entity> hgRdr3OldList = this.hgOrdrService.findHql(hql2,dOCENTRY2);
					page.setHgRdr3List(hgRdr3OldList);
					pageList.add(page);
				}catch(Exception e){
					logger.info(e.getMessage());
				}
			}
		}
		return Result.success(pageList);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据ID获取报关订单信息",notes="根据ID获取报关订单信息",httpMethod="GET",produces="application/json")
	public ResponseMessage<?> get(@ApiParam(required=true,name="id",value="ID")@PathVariable("id") String id) {
		HgOrdrEntity task = hgOrdrService.get(HgOrdrEntity.class, id);
		if (task == null) {
			return Result.error("根据ID获取报关订单信息为空");
		}
		HgOrdrPage page = new HgOrdrPage();
		try {
			MyBeanUtils.copyBeanNotNull2Bean(task, page);
			Object dOCENTRY0 = task.getId();
			Object dOCENTRY1 = task.getId();
			Object dOCENTRY2 = task.getId();
			String hql0 = "from HgRdr1Entity where 1 = 1 AND dOCENTRY = ? ";
			List<HgRdr1Entity> hgRdr1OldList = this.hgOrdrService.findHql(hql0,dOCENTRY0);
			page.setHgRdr1List(hgRdr1OldList);
			String hql1 = "from HgRdr2Entity where 1 = 1 AND dOCENTRY = ? ";
			List<HgRdr2Entity> hgRdr2OldList = this.hgOrdrService.findHql(hql1,dOCENTRY1);
			page.setHgRdr2List(hgRdr2OldList);
			String hql2 = "from HgRdr3Entity where 1 = 1 AND dOCENTRY = ? ";
			List<HgRdr3Entity> hgRdr3OldList = this.hgOrdrService.findHql(hql2,dOCENTRY2);
			page.setHgRdr3List(hgRdr3OldList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.success(page);
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="创建报关订单")
	public ResponseMessage<?> create(@ApiParam(name="报关订单对象")@RequestBody HgOrdrPage hgOrdrPage, UriComponentsBuilder uriBuilder) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<HgOrdrPage>> failures = validator.validate(hgOrdrPage);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}

		//保存
		List<HgRdr1Entity> hgRdr1List =  hgOrdrPage.getHgRdr1List();
		List<HgRdr2Entity> hgRdr2List =  hgOrdrPage.getHgRdr2List();
		List<HgRdr3Entity> hgRdr3List =  hgOrdrPage.getHgRdr3List();

		HgOrdrEntity hgOrdr = new HgOrdrEntity();
		try{
			MyBeanUtils.copyBeanNotNull2Bean(hgOrdrPage,hgOrdr);
		}catch(Exception e){
			logger.info(e.getMessage());
			return Result.error("保存报关订单失败");
		}
		hgOrdrService.addMain(hgOrdr, hgRdr1List,hgRdr2List,hgRdr3List);

		return Result.success(hgOrdr);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="更新报关订单",notes="更新报关订单")
	public ResponseMessage<?> update(@RequestBody HgOrdrPage hgOrdrPage) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<HgOrdrPage>> failures = validator.validate(hgOrdrPage);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}

		//保存
		List<HgRdr1Entity> hgRdr1List =  hgOrdrPage.getHgRdr1List();
		List<HgRdr2Entity> hgRdr2List =  hgOrdrPage.getHgRdr2List();
		List<HgRdr3Entity> hgRdr3List =  hgOrdrPage.getHgRdr3List();

		HgOrdrEntity hgOrdr = new HgOrdrEntity();
		try{
			MyBeanUtils.copyBeanNotNull2Bean(hgOrdrPage,hgOrdr);
		}catch(Exception e){
			logger.info(e.getMessage());
			return Result.error("报关订单更新失败");
		}
		hgOrdrService.updateMain(hgOrdr, hgRdr1List,hgRdr2List,hgRdr3List);

		//按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return Result.success();
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ApiOperation(value="删除报关订单")
	public ResponseMessage<?> delete(@ApiParam(name="id",value="ID",required=true)@PathVariable("id") String id) {
		logger.info("delete[{}]" + id);
		// 验证
		if (StringUtils.isEmpty(id)) {
			return Result.error("ID不能为空");
		}
		try {
			HgOrdrEntity hgOrdr = hgOrdrService.get(HgOrdrEntity.class, id);
			hgOrdrService.delMain(hgOrdr);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("报关订单删除失败");
		}

		return Result.success();
	}
}
